#include <stdio.h>
#define STEP	   1
#define MAXDIMLEN  10
#define NRW        24     // number of reserved words
#define TXMAX      500    // length of identifier table
#define MAXNUMLEN  14     // maximum number of digits in numbers
#define NSYM       12     // maximum number of symbols in array ssym and csym
#define MAXIDLEN   10     // length of identifiers

#define MAXADDRESS 32767  // maximum address
#define MAXLEVEL   32     // maximum depth of nesting block
#define CXMAX      500    // size of code array

#define MAXINS   10

#define MAXSYM     30     // maximum number of symbols  

#define STACKSIZE  1000   // maximum storage

enum symtype
{
/* 00 */	SYM_NULL,
/* 01 */	SYM_IDENTIFIER,
/* 02 */	SYM_NUMBER,
/* 03 */	SYM_PLUS,
/* 04 */	SYM_MINUS,
/* 05 */	SYM_TIMES,
/* 06 */	SYM_SLASH,
/* 07 */	SYM_ODD,
/* 08 */	SYM_EQU,
/* 09 */	SYM_NEQ,
/* 10 */	SYM_LES,
/* 11 */	SYM_LEQ,
/* 12 */	SYM_GTR,
/* 13 */	SYM_GEQ,
/* 14 */	SYM_LPAREN,
/* 15 */	SYM_RPAREN,
/* 16 */	SYM_COMMA,
/* 17 */	SYM_SEMICOLON,
/* 18 */	SYM_PERIOD,
/* 19 */	SYM_BECOMES,
/* 20 */	SYM_BEGIN,
/* 21 */	SYM_END,
/* 22 */	SYM_IF,
/* 23 */	SYM_THEN,
/* 24 */	SYM_WHILE,
/* 25 */	SYM_DO,
/* 26 */	SYM_CALL,
/* 27 */	SYM_CONST,
/* 28 */	SYM_VAR,
/* 29 */	SYM_PROCEDURE,
/* 30 */	SYM_WRITE,
/* 31 */	SYM_READ,
/* 32 */	SYM_FOR,
/* 33 */	SYM_DOWNTO,
/* 34 */	SYM_TO,
/* 35 */	SYM_ELSE,
/* 36 */	SYM_WRITELN,
/* 37 */	SYM_REPEAT,
/* 38 */	SYM_UNTIL,
/* 39 */	SYM_EXIT,
/* 40 */	SYM_AND,
/* 41 */	SYM_OR,
/* 42 */	SYM_NOT,
/* 43 */	SYM_LSQUARE,
/* 44 */	SYM_RSQUARE
};

enum idtype
{
	ID_CONSTANT, ID_VARIABLE, ID_PROCEDURE,ID_ARRAY
};

enum opcode
{
	LIT, OPR, LOD, STO, CAL, INT, JMP, JPC,STA,LAD
};

enum oprcode
{
	OPR_RET, OPR_NEG, OPR_ADD, OPR_MIN,
	OPR_MUL, OPR_DIV, OPR_ODD, OPR_EQU,
	OPR_NEQ, OPR_LES, OPR_LEQ, OPR_GTR,
	OPR_GEQ, OPR_WTL, OPR_PRT, OPR_RED,
	OPR_AND, OPR_OR,  OPR_NOT
};


typedef struct
{
	int f; // function code
	int l; // level
	int a; // displacement address
} instruction;

//////////////////////////////////////////////////////////////////////
char* err_msg[] =
{
/*  0 */    "",
/*  1 */    "Found ':=' when expecting '='.",
/*  2 */    "There must be a number to follow '='.",
/*  3 */    "There must be an '=' to follow the identifier.",
/*  4 */    "There must be an identifier to follow 'for', 'const', 'var', or 'procedure'.",
/*  5 */    "Missing ',' or ';'.",
/*  6 */    "Incorrect procedure name.",
/*  7 */    "Statement expected.",
/*  8 */    "Follow the statement is an incorrect symbol.",
/*  9 */    "'.' expected.",
/* 10 */    "';' expected.",
/* 11 */    "Undeclared identifier.",
/* 12 */    "Illegal assignment.",
/* 13 */    "':=' expected.",
/* 14 */    "There must be an identifier to follow the 'call'.",
/* 15 */    "A constant or variable can not be called.",
/* 16 */    "'then' expected.",
/* 17 */    "';' or 'end' expected.",
/* 18 */    "'do' expected.",
/* 19 */    "Incorrect symbol.",
/* 20 */    "Relative operators expected.",
/* 21 */    "Procedure identifier can not be in an expression.",
/* 22 */    "Missing ')'.",
/* 23 */    "The symbol can not be followed by a factor.",
/* 24 */    "The symbol can not be as the beginning of an expression.",
/* 25 */    "The number is too great.",
/* 26 */    "",
/* 27 */    "']' expected.",
/* 28 */    "'[' expected.",
/* 29 */    "the symbol can not be as the beginning of the condition",
/* 30 */    "'to' or 'downto' expected.",
/* 31 */    "missing until",
/* 32 */    "There are too many levels.",
/* 33 */	 "')' expected." ,
/* 34 */    "The number of segments doesn't match."

};

//////////////////////////////////////////////////////////////////////
char ch;         // last character read
int  sym;        // last symbol read
char id[MAXIDLEN + 1]; // last identifier read
int  num;        // last number read
int  cc;         // character count
int  ll;         // line length
int  kk;
int  err;
int  cx;         // index of current instruction to be generated.
int  level = 0;
int  tx = 0;
int dim=0;
int array_size=1;
int latit[MAXDIMLEN];

char line[80];

instruction code[CXMAX];

char* word[NRW + 1] =
{
	"", /* place holder */
	"begin", "call", "const", "do", "end","if", //6
	"odd", "procedure", "then", "var", "while", //11
	"write","read","for","downto","to","else", 	//17
	"writeln","repeat","until","exit","and",	//22
	"or","not"
};

int wsym[NRW + 1] =
{
	SYM_NULL, SYM_BEGIN, SYM_CALL, SYM_CONST, SYM_DO, SYM_END,
	SYM_IF, SYM_ODD, SYM_PROCEDURE, SYM_THEN, SYM_VAR, SYM_WHILE,
	SYM_WRITE,SYM_READ,SYM_FOR,SYM_DOWNTO,SYM_TO,SYM_ELSE,
	SYM_WRITELN,SYM_REPEAT,SYM_UNTIL,SYM_EXIT,SYM_AND,
	SYM_OR,SYM_NOT
};

int ssym[NSYM + 1] =
{
	SYM_NULL, SYM_PLUS, SYM_MINUS, SYM_TIMES, SYM_SLASH,
	SYM_LPAREN, SYM_RPAREN, SYM_EQU, SYM_COMMA, SYM_PERIOD, SYM_SEMICOLON,
	SYM_LSQUARE,SYM_RSQUARE
};

char csym[NSYM + 1] =
{
	' ', '+', '-', '*', '/', '(', ')', '=', ',', '.', ';','[',']'
};

char* mnemonic[MAXINS] =
{
	"LIT", "OPR", "LOD", "STO", "CAL", "INT", "JMP", "JPC","STA","LAD"
};

typedef struct dim{
	int dim_len;
	struct dim* next;
}p_dim;

typedef struct
{
	char name[MAXIDLEN + 1];
	int  kind;
	int  value;
	int numOfPar;
	p_dim* next;
} comtab;

comtab table[TXMAX];

typedef struct
{
	char  name[MAXIDLEN + 1];
	int   kind;
	short level;
	short address;
	int numOfPar;
	p_dim* next;
} mask;

typedef struct
{
	char name[MAXIDLEN+1];
	short kind;
	short dim_n;
	short level;
	short address;
	int numOfPar;
	p_dim* next;
}array;

FILE* infile;

// EOF PL0.h
